--
-- Author: shunguo.chen
-- Date: 2016-08-09 13:53:57
--
-- MailRender

local ClassRef = app.mvc.createRenderClass(...)

--data = MailVo
function ClassRef.create(data, node)
	local sprite = ClassRef.extend(node or cc.CSLoader:createNode(Res.MailNode))
	if node == nil then
		ccui.Helper:doLayout(sprite)
	end
	sprite:init(data)
	return sprite
end       

function ClassRef:init(data)
	self.super.init(self, data)

	self:setContentSize(self._ui.Panel:getContentSize())
	self:setSelected(false)
	display.uiAddClick(self._ui.Panel, function()
			if not self:isSelected() then
				self:setSelected(true)
				executeFunc(self._onBtnClick, self)
			end
		end)
	self:refresh()
end

function ClassRef:refresh()
	if self._data then
		local mail = self._data
		self._ui.Text_name:setString(mail:getSubjectBriefName())
		self._ui.Text_from:setString(mail.sender)
		self._ui.num:setString(mail:getLeftDays())

		self._ui.Image_fujian:setVisible(mail:canGetItems())

		local isNew = mail:isNew()
		self._ui.tishi:setVisible(isNew)
		self._ui.icon:setVisible(isNew)
		self._ui.Image_over:setVisible(not isNew)
	end
end

function ClassRef:onBtnClick(cb)
	self._onBtnClick = cb
end

function ClassRef:setSelected(b)
	self._ui.selected:setVisible(b)
	local color = b and cc.c3b(47,12,4) or cc.c3b(176,176,176)
	self._ui.Text_from:setTextColor(color)
	self._ui.Text_from_tlt:setTextColor(color)
	self._ui.Text_day_tlt:setTextColor(color)
end

function ClassRef:isSelected()
	return self._ui.selected:isVisible()
end

return ClassRef